package com.brillilab.service.core.experiments.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.experiments.PlateGridReagentMapper;
import com.brillilab.dao.mapper.experiments.PlateMapper;
import com.brillilab.domain.enums.common.BoolEnum;
import com.brillilab.domain.po.experiments.Plate;
import com.brillilab.domain.po.experiments.PlateGridReagent;
import com.brillilab.domain.vo.experiments.Alias;
import com.brillilab.domain.vo.experiments.PlateGridReagentVo;
import com.brillilab.service.core.experiments.IPlateGridReagentService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Transactional
public class PlateGridReagentServiceImpl implements IPlateGridReagentService {

    @Resource
    private PlateMapper plateMapper;
    @Resource
    private PlateGridReagentMapper plateGridReagentMapper;

    @Override
    public boolean insert(PlateGridReagent gridReagent) {
        return plateGridReagentMapper.insert(gridReagent) > 0;
    }

    @Override
    public int selectCountOnePlate(Long plateId) {
        return plateGridReagentMapper.selectCount(
                new LambdaQueryWrapper<PlateGridReagent>()
                        .eq(PlateGridReagent::getPlateId,plateId));
    }

    @Override
    public PlateGridReagent selectOne(Long plateId,Long gridId,Long stepReagentId,Integer type) {

        LambdaQueryWrapper<PlateGridReagent> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PlateGridReagent::getPlateId,plateId);
        wrapper.eq(PlateGridReagent::getPlateGridId,gridId);
        wrapper.eq(PlateGridReagent::getSourceId,stepReagentId);
        wrapper.eq(PlateGridReagent::getType,type);

        return plateGridReagentMapper.selectOne(wrapper);
    }

    @Override
    public List<PlateGridReagent> selectList(Long plateId) {
        return plateGridReagentMapper.selectListByPlateId(plateId);
    }

    @Override
    public List<PlateGridReagentVo> selectVoList(Long plateId,Long gridId,Integer classify) {
        return plateGridReagentMapper.selectVoList(plateId,gridId,classify);
    }

    @Override
    public void updateAlias(Long sourceId,Integer type,String alias){
        LambdaQueryWrapper<PlateGridReagent> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PlateGridReagent::getSourceId,sourceId);
        wrapper.eq(PlateGridReagent::getType,type);
        PlateGridReagent plateGridReagent=new PlateGridReagent();
        plateGridReagent.setAlias(alias);
        int i=plateGridReagentMapper.update(plateGridReagent,wrapper);
        Assert.isTrue(i>0,"操作失败！");
    }

    @Override
    public boolean deleteByPlateId(Long plateId) {
        return plateGridReagentMapper.delete(
                new LambdaQueryWrapper<PlateGridReagent>()
                        .eq(PlateGridReagent::getPlateId,plateId)) > 0;
    }

    @Override
    public int selectCountOneGrid(Long gridId) {
        return plateGridReagentMapper.selectCount(
                new LambdaQueryWrapper<PlateGridReagent>()
                        .eq(PlateGridReagent::getPlateGridId,gridId));
    }

    @Override
    public PlateGridReagent selectOne(Long stepReagentId,Integer type,Long gridId) {
        LambdaQueryWrapper<PlateGridReagent> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(PlateGridReagent::getSourceId,stepReagentId);
        wrapper.eq(PlateGridReagent::getType,type);
        wrapper.eq(PlateGridReagent::getPlateGridId,gridId);
        return plateGridReagentMapper.selectOne(wrapper);
    }

    @Override
    public boolean deleteById(Long id) {
        return plateGridReagentMapper.deleteById(id) > 0;
    }

    @Override
    public List<PlateGridReagent> selectList(List<Long> gridIds) {
        if(gridIds.size()>0){
            return plateGridReagentMapper.selectList(
                    new LambdaQueryWrapper<PlateGridReagent>()
                            .in(PlateGridReagent::getPlateGridId,gridIds));
        }
        return new ArrayList<>();
    }

    @Override
    public int deleteInGridIds(Long sourceId,Integer type,List<Long> gridIds) {
        if(gridIds.size()>0){
            return plateGridReagentMapper.delete(
                    new LambdaQueryWrapper<PlateGridReagent>()
                            .eq(PlateGridReagent::getSourceId,sourceId)
                            .eq(PlateGridReagent::getType,type)
                            .in(PlateGridReagent::getPlateGridId,gridIds));
        }
        return 0;
    }

    @Override
    public int insertBatch(List<PlateGridReagent> insertRe) {
        if(insertRe.size()>0){
            return plateGridReagentMapper.insertBatch(insertRe);
        }
        return 0;
    }

    @Override
    public int updateBatch(List<PlateGridReagent> updateRe) {
        if(updateRe.size()>0){
            return plateGridReagentMapper.updateBatch(updateRe);
        }
        return 0;
    }

    @Override
    public int updateAliasBatch(List<Alias> aliases) {
        if(aliases.size()>0){
            return plateGridReagentMapper.updateAliasBatch(aliases);
        }
        return 0;
    }

    @Override
    public int deleteBySourceIdAndType(List<Long> sourceIds,Integer type) {
        if(sourceIds!=null && sourceIds.size()>0){
            return plateGridReagentMapper.delete(
                    new LambdaQueryWrapper<PlateGridReagent>()
                            .in(PlateGridReagent::getSourceId,sourceIds)
                            .eq(PlateGridReagent::getType,type));
        }
        return 0;
    }

    @Override
    public List<PlateGridReagent> selectListBySourceIdAndType(List<Long> sourceIds,Integer type) {
        return plateGridReagentMapper.selectList(
                new LambdaQueryWrapper<PlateGridReagent>()
                        .in(PlateGridReagent::getSourceId,sourceIds)
                        .eq(PlateGridReagent::getType,type));
    }

    @Override
    public List<PlateGridReagentVo> selectVoList(List<Long> gridIds) {
        return plateGridReagentMapper.selectVoListByGridIds(gridIds);
    }

    @Override
    public List<Long> selectPlateGridIdList(Long plateId) {
        return plateGridReagentMapper.selectPlateGridIdList(plateId);
    }

    @Override
    public boolean deleteByExperimentId(Long experimentId) {
        List<Plate> plateList=plateMapper.selectList(
                new LambdaQueryWrapper<Plate>()
                        .eq(Plate::getExperimentId,experimentId)
                        .eq(Plate::getIsDelete,BoolEnum.FALSE.getValue()));
        List<Long> plateIds=plateList.stream().map(Plate::getId).distinct().collect(Collectors.toList());
        if(CollectionUtils.isNotEmpty(plateIds)){
            return plateGridReagentMapper.delete(
                    new LambdaQueryWrapper<PlateGridReagent>()
                            .in(PlateGridReagent::getPlateId,plateIds))>0;
        }
        return true;
    }
}
